<?php

namespace App\Http\Controllers\Api\Virtual;

use App\Enums\Api\Response;
use App\Http\Controllers\Controller;
use App\Models\Category\Category;
use App\Models\Category\CategoryModule;
use App\Models\Tool\FileUrl;
use App\Models\Virtual\Virtual;
use App\Models\Virtual\VirtualWord;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class VirtualController extends Controller
{
    public function list(Request $request):JsonResponse
    {
        $category = Category::query()->select('id', 'parent_id')->with('virtual_unit:unit_id,unit_name,unit_main_name')->has('virtual_unit')->where('parent_id', $request->input('gradeId'))->get()->toArray();
        $new_array = [];
        foreach ($category as $v) {
            $array['unit_id']=$v['id'];
            $array['unit_name']=$v['virtual_unit'][0]['unit_name'];
            $array['unit']=$v['virtual_unit'][0]['unit_main_name'];
            $new_array[]=$array;

        }
        return apiResponse(code:Response::SERVICE_SUCCESS->value,data:$new_array,msg: Response::SERVICE_SUCCESS->response(),type: 'virtual');
    }
    public function page(int $unitId)
    {
        if (!Virtual::query()->select('id','unit_name','page_number','title','mobile_image','image','video','is_read')
            ->where('unit_id',$unitId)->exists()){
            return apiResponse(code:Response::SYSTEM_DATA_NOT_EXISTS->value,data:[],msg: Response::SYSTEM_DATA_NOT_EXISTS->response(),type: 'virtual_page');
        }
        $gradeId=Category::query()->where('id',$unitId)->value('parent_id');
        $virtual=Virtual::query()->select('id','unit_name','page_number','title','mobile_image','image','video','is_read')
            ->where('unit_id',$unitId)
            ->get();
        $file_url=FileUrl::query()->where('module',CategoryModule::ModuleTypeOne)->first();
        foreach ($virtual as &$v) {
            $v->mobile_image=convert_file_address_exist(module:CategoryModule::ModuleTypeOne,file_address:$v->mobile_image,grade:$gradeId,category:$unitId,type: '.png',file_url: $file_url);
            $v->image=convert_file_address_exist(module:CategoryModule::ModuleTypeOne,file_address:$v->image,grade:$gradeId,category:$unitId,type: '.png',file_url: $file_url);
            $v->video=convert_file_address_exist(module:CategoryModule::ModuleTypeOne,file_address:$v->video,grade:$gradeId,category:$unitId,type: '.mp4',file_url: $file_url);
        }
        return apiResponse(code:Response::SERVICE_SUCCESS->value,data:$virtual,msg: Response::SERVICE_SUCCESS->response(),type: 'virtual_page');
    }
    public function word(int $pageId):JsonResponse
    {
        if (!VirtualWord::query()->with('belongsTo')->where('virtual_id',$pageId)->exists()){
            return apiResponse(code:Response::SYSTEM_DATA_NOT_EXISTS->value,data:[],msg: Response::SYSTEM_DATA_NOT_EXISTS->response(),type: 'virtual_page');
        }
        $virtualWords=VirtualWord::query()->with('virtual:id,unit_id,unit_name,unit_main_name,category_id','click_learn:id,content,translation,audio_url,type')
            ->where('virtual_id',$pageId)->get()->toArray();
        $file_url=FileUrl::query()->where('module',CategoryModule::ModuleTypeOne)->first();

        $data=array_map(function($v)use($file_url){
            $data['unit_id']=$v['virtual']['unit_id'];
            $data['unit_name']=$v['virtual']['unit_name'];
            $data['unit']=$v['virtual']['unit_main_name'];
            $data['readType']=$v['type'];
            $data['time']=$v['time'];

            $data['word']=[
                'id'=>$v['click_learn']['id'],
                'content'=>$v['click_learn']['content'],
                'translation'=>$v['click_learn']['translation'],
                'audioName'=>convert_file_address_exist(module:CategoryModule::ModuleTypeOne,file_address:$v['click_learn']['audio_url'],grade:$v['virtual']['category_id'],category:$v['virtual']['unit_id'],type: '.mp3',file_url: $file_url),
                'type'=>$v['click_learn']['type'],
            ];
            return $data;
        },$virtualWords);
        return apiResponse(code:Response::SERVICE_SUCCESS->value,data:$data,msg: Response::SERVICE_SUCCESS->response(),type: 'virtual_word');
    }
}
